package com.zeroxml.cms.web.action.admin;

import com.zeroxml.cms.bo.Catalog;
import com.zeroxml.cms.service.ICatalogService;
import com.zeroxml.cms.web.action.BaseAdminAction;
import com.wyvoid.core.sitemesh.Partial;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.integration.spring.SpringBean;

import java.util.List;

/**
 * User: cjp
 * Date: 2007-9-9
 * Time: 21:53:14
 */
@UrlBinding("/admin/catalogManage.do")
public class CatalogManageAction extends BaseAdminAction {
    @SpringBean
    private ICatalogService catalogService;
    private Catalog catalog;
    private List<Catalog> catalogs;
    private long id;

    public void setCatalogService(ICatalogService catalogService) {
        this.catalogService = catalogService;
    }

    public List<Catalog> getCatalogs() {
        return catalogs;
    }

    public Catalog getCatalog() {
        return catalog;
    }

    public void setCatalog(Catalog catalog) {
        this.catalog = catalog;
    }

    public void setId(long id) {
        this.id = id;
    }

    @DefaultHandler
    @Partial
    public Resolution catalogList(){
        catalogs=catalogService.getCatalogList(id);
        return getAdminResolution("catalogList");
    }

    public Resolution addCatalogForm() {
        catalogs=catalogService.getCatalogList(0);
        if (id > 0) {
            catalog = catalogService.getCatalog(id);
        }
        return getAdminResolution("addCatalogForm");
    }

    public Resolution addCatalog() {
        if (id > 0) {
            Catalog tempCatalog = catalogService.getCatalog(id);
            tempCatalog.setName(catalog.getName());
            tempCatalog.setParentCatalog(catalog.getParentCatalog());
            catalogService.updateCatalog(tempCatalog);
        } else {
            catalogService.createCatalog(catalog);
        }
        return new RedirectResolution(getClass());
    }

    public Resolution deleteCatalog(){
        catalogService.deleteCatalog(id);
        return new RedirectResolution(getClass());
    }
}
